// electron/electron.js
// import path from 'path'
import { app, BrowserWindow, Menu, shell, dialog, ipcMain, IpcMainInvokeEvent } from 'electron'
import * as fs from 'fs'
import * as path from 'path'

const isDev = process.env.IS_DEV == "true" ? true : false

function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      devTools: true,
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true,
      webSecurity: false,
    },
  })
  const Menus = [
    {
      label:'刷新',
      role: 'help',
      click: () => {
        mainWindow.webContents.reload()
      }
    },
    {
      label:'注册本地插件',
      role: 'help',
      click: () => {
        const nowPath = app.getPath('exe').replace('Neu.exe','').replace(/\\/g,'\\\\')+'resources\\\\extraResources\\\\'
        const regFile = nowPath + 'makein.reg'
        const exeFile = nowPath + 'Neu.exe'
        const regText = `Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\\yyfftool]
@="URL:yyfftool Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\\yyfftool\\DefaultIcon]
@="`+exeFile+`"

[HKEY_CLASSES_ROOT\\yyfftool\\shell]

[HKEY_CLASSES_ROOT\\yyfftool\\shell\\open]

[HKEY_CLASSES_ROOT\\yyfftool\\shell\\open\\command]
@="`+exeFile+` \\"%1\\""`
        
        fs.writeFile(regFile, regText,  (err)=> {
          if (err) {
            dialog.showMessageBox(mainWindow,{
              type: "info",
              title: "提示",
              message: err.message,
              buttons:["确定"],
            }).then((index)=>{
              // if(index.response === 1){
              //   app.exit()
              // }
            })
          }
          shell.openPath(regFile)
        })
      }
    },
    // {
    //   label:'开发者工具',
    //   role: 'help',
    //   click: () => {
    //     mainWindow.webContents.openDevTools()
    //   }
    // },
]
  // @ts-ignore
  const mainMenu = Menu.buildFromTemplate(Menus)
  Menu.setApplicationMenu(mainMenu)
  // and load the index.html of the app.
  // win.loadFile("index.html")
  mainWindow.loadURL(
    isDev
      ? 'http://localhost:8888'
      : `file://${path.join(__dirname, '../dist/index.html')}`
  )
  // Open the DevTools.
  // if (isDev) {
  //   mainWindow.webContents.openDevTools()
  // }
  // mainWindow.webContents.openDevTools()
}

async function handleTelnet (_event: IpcMainInvokeEvent, host: string, port?: number) {
  return '222'
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  ipcMain.handle('net:telnet', handleTelnet)
  createWindow()
  app.on('activate', () => {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})
 
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
